package com.ssyc.gsk_master.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.utils.TextUtils;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.IndicatorManager;
import com.ssyc.common.manager.StateBarManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.vp.SuperViewPager;
import com.ssyc.gsk_master.R;
import com.ssyc.gsk_master.bean.TeacherInfo;
import com.ssyc.gsk_master.fragment.MasterAllEventFragment;
import com.ssyc.gsk_master.fragment.MasterEventFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 活动
 * Created by Administrator on 2018/7/16 0016.
 */

public class MasterEventActivity extends BaseActivity implements View.OnClickListener {

    //返回
    private RelativeLayout rlBack;
    private MagicIndicator mMag;
    private SuperViewPager mVp;
    //编辑
    private RelativeLayout rlEdit;

    //加载框
    private RelativeLayout rlLoading;

    //内容布局
    private LinearLayout llContent;

    //导航栏的标题集合
    private String[] mTitles;

    //用于填充ViewPager的Fragment集合
    private List<Fragment> mFragments;


    //界面刷新的标识
    public static final int UPDATASCORE = 0x450;

    //适配器对象
    private VpAdapter adapter;

    //所有老师的集合
    private List<TeacherInfo.ListBean> teacherInfos;


    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.master_activity_event;
    }

    @Override
    protected void init() {

        showContent();

        //配置状态栏颜色
        initStateColor();

        //初始化视图
        initView();

        //获取所有老师
        http();
    }


    /**
     * 初始化视图的方法
     */
    public void initView() {
        mMag = findViewById(R.id.mag);
        mVp = findViewById(R.id.vp);
        rlBack = findViewById(R.id.rl_back);
        rlBack.setOnClickListener(this);
        rlEdit = findViewById(R.id.rl_edit);
        rlEdit.setOnClickListener(this);
        rlLoading = findViewById(R.id.rl_loading);
        llContent = findViewById(R.id.ll_conent);
        llContent.setVisibility(View.GONE);
    }

    /**
     * 配置状态栏颜色的方法
     */
    private void initStateColor() {
        //设置6.0以后沉浸状体栏的问题
        StateBarManager.setBarWithUltimateBar(this, getResources().getColor(com.ssyc.common.R.color.titlebg),
                getResources().getColor(com.ssyc.common.R.color.nav_color));
    }

    /**
     * 获取所有老师的接口
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "15");
        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "8");
        HttpUtils.post(HttpAdress.GETTEACHERS, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (null != rlLoading && rlLoading.getVisibility() != View.GONE) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (null != llContent && llContent.getVisibility() != View.VISIBLE) {
                    llContent.setVisibility(View.VISIBLE);
                }


                if (!TextUtils.isEmpty(response)) {
                    TeacherInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, TeacherInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("1".equals(info.getState())) {
                            //返回成功
                            teacherInfos = info.getList();
                            if (null != teacherInfos && teacherInfos.size() != 0) {
                                mTitles = new String[teacherInfos.size() + 1];
                                for (int i = 0; i < mTitles.length; i++) {
                                    if (i == 0) {
                                        mTitles[0] = "全部";
                                    } else {
                                        mTitles[i] = teacherInfos.get(i - 1).getName();
                                    }
                                }

                                IndicatorManager.setDynamicIndicator(MasterEventActivity.this, mTitles, mVp, mMag, "#999999", "#1ABC9C");

                                //初始化Vp
                                initVp();
                            }
                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }
                    } else {
                        Log.i("test", Constants.EMPTY);
                    }
                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }


    /**
     * 初始化ViewPager的方法
     */
    private void initVp() {
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            if (i == 0) {
                mFragments.add(MasterAllEventFragment.newInstance());
            } else {
                //携带班级号过去
                mFragments.add(MasterEventFragment.newInstance(teacherInfos.get(i - 1).getUserid() + ""));
            }
        }
        adapter = new VpAdapter(getSupportFragmentManager());
        mVp.setAdapter(adapter);
        mVp.setOffscreenPageLimit(mFragments.size());
    }

    /**
     * 页面控件点击事件
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.rl_back) {
            //返回
            finish();
        }
    }

    /**
     * ViewPager的适配器
     */
    class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }

}
